package com.lingxinst.farm.base.ui

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
/**
 * @description:
 * @author: Lynn
 * @datetime: 2025/6/10 12:38
 **/
class BaseSingleViewHolder<VDB : ViewDataBinding>(val binding: VDB) : RecyclerView.ViewHolder(binding.root)

/**
 * @description:
 * @author: bailing
 * @datetime: 2025/5/28 20:04
 **/
abstract class BaseSingleAdapter<T, VDB : ViewDataBinding>(private val dataList: MutableList<T> = mutableListOf<T>()) :
    RecyclerView.Adapter<BaseSingleViewHolder<VDB>>() {

    abstract fun getItemLayoutId(): Int

    fun setDataList(dataList: List<T>) {
        this.dataList.clear()
        this.dataList.addAll(dataList)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseSingleViewHolder<VDB> {
        val binding: VDB = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            getItemLayoutId(),
            parent,
            false
        )
        return BaseSingleViewHolder<VDB>(binding)
    }

    override fun onBindViewHolder(holder: BaseSingleViewHolder<VDB>, position: Int) {
        val item = dataList[position]
        bindData(holder.binding, item, position)
    }

    abstract fun bindData(binding: VDB, t: T, position: Int)


    /**
     * 子类复写
     */
//    override fun onBindViewHolder(holder: BaseSingleViewHolder<VDB>, position: Int) {
//
//    }

    override fun getItemCount(): Int {
        return dataList.size
    }




}